﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using The_Square_Saga.Objects.Interface;
using The_Square_Saga.Utils;

namespace The_Square_Saga.Objects
{
    public enum PlayerState
    {
        stay,
        walk,
        jump,
        attack,
        defense
    }

    class Player : AnimatedGameEntity, IGameEntity
    {
        private PlayerState currentState;


        private float velocity;

        public Player(Texture2D image, Vector2 position, float velocity, Vector2 frameSize)
            : base(image, position)
        {
            this.velocity = velocity;
            this.currentState = PlayerState.stay;
            this.direction = Vector2.Zero;
            
        }
        
        /// <summary>
        /// Deal with the input, set direction and player state
        /// </summary>
        private void handleInput()
        {
            PlayerState pState = currentState;
            direction = Vector2.Zero;

            if (Game1.keyboardState.IsKeyDown(Keys.Right))
            {
                pState = PlayerState.walk;
                direction = new Vector2(1, 0);
            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Left))
            {
                pState = PlayerState.walk;
                direction = new Vector2(-1, 0);
            }
            else pState = PlayerState.stay;

            if (pState != currentState)
            {
                currentState = pState;
                // flip the image if player goes to left
                if (direction.X == -1)
                {
                    this.flipHorizontally();
                }
                else if (direction.X == 1)
                {
                    this.deFlipHorizontally();
                }
            }
        }

        public override void update(GameTime gameTime)
        {
            handleInput();

            this.position += velocity * direction;

            if (currentState == PlayerState.walk)
            {
                //this.playAnimation("walk",gameTime);
            }

            base.update(gameTime);
        }
    }
}
